<?php

namespace JsonLowCode;

use JsonLowCode\Helps\Random;
use JsonLowCode\Support\Fluent;

class Axios extends Fluent
{
    protected array $attributes = [
        'isInit'=>false,
        'type'=>"fetch",
        'options'=>[
            'uri'=>"",
            'isCors'=>true,
            'timeout'=>600,
            'method'=>'GET',
            'headers'=>[],
            'params'=>[]
        ]
    ];

    public function __construct()
    {
        parent::__construct($this->attributes);
        $this->id(Random::alpha(8));
    }

    /**
     * 设置ID
     * @param string $id
     * @return $this
     */
    public function id(string $id): static
    {
        $this->attributes["id"] = $id;
        return $this;
    }

    /**
     * 设置isInit 是否立即执行
     * @param bool $isInit
     * @return $this
     */
    public function isInit(bool $isInit = true): static
    {
        $this->attributes['isInit'] = $isInit;
        return $this;
    }

    /**
     * 设置Schema属性值
     * @param string $key
     * @param mixed $value
     * @return $this
     */
    public function options(string $key,mixed $value): static
    {
        $this->attributes['options'][$key] = $value;
        return $this;
    }


    /**
     * 设置uri请求信息
     * @param string $uri
     * @return $this
     */
    public function uri(string $uri): static
    {
        return $this->options('uri',$uri);
    }

    /**
     * 设置method信息
     * @param string $method
     * @return $this
     */
    public function method(string $method = 'GET'|'POST'|'PUT'|'DELETE'): static
    {
        return $this->options('method',$method);
    }

    /**
     * 设置uri请求信息
     * @param array $params
     * @return $this
     */
    public function params(array $params): static
    {
        return $this->options('params',$params);
    }

    /**
     * 设置header信息
     * @param string $key
     * @param string $value
     * @return $this
     */
    public function param(string $key, string $value): static
    {
        $params = $this->attributes['options']['params'];
        $params[$key] = $value;
        return $this->params($params);
    }

    /**
     * 设置isInit 是否立即执行
     * @param bool $isCors
     * @return $this
     */
    public function isCors(bool $isCors = true): static
    {
        return $this->options('isCors',$isCors);
    }

    /**
     * 设置超时时间
     * @param int $timeout
     * @return $this
     */
    public function timeout(int $timeout = 600): static
    {
        return $this->options('timeout',$timeout);
    }

    /**
     * 设置header信息
     * @param array $value
     * @return $this
     */
    public function headers(array $value): static
    {
        $this->options('headers',$value);
        return $this;
    }

    /**
     * 设置header信息
     * @param string $key
     * @param string $value
     * @return $this
     */
    public function header(string $key, string $value): static
    {
        $headers = $this->attributes['options']['headers'];
        $headers[$key] = $value;
        return $this->headers($headers);
    }
}
